package com.freesky.biz.icbc.controller;

import com.freesky.biz.icbc.domain.TScanBusi;
import com.freesky.biz.icbc.service.IScanBusiService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.TScanChannel;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;

import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 线下扫码业务(业务管理）
 */
@RestController
@RequestMapping("/icbc/scanbusi")
public class ScanBusiController extends BaseController {

    @Autowired
    private IScanBusiService scanBusiService;

    /**
     * 获取扫码业务列表
     */
    @PreAuthorize("@ss.hasPermi('icbc:scanbusi:list')")
    @GetMapping("/page")
    public TableDataInfo page(TScanBusi scanBusi) {
        startPage();
        List<TScanBusi> list = scanBusiService.selectScanBusiList(scanBusi);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('icbc:scanbusi:list')")
    @GetMapping("/list")
    public AjaxResult list(TScanBusi scanBusi) {
        List<TScanBusi> list = scanBusiService.selectScanBusiList(scanBusi);
        return success(list);
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('icbc:scanbusi:query')")
    @GetMapping(value = "/{scanBusiId}")
    public AjaxResult getInfo(@PathVariable Long scanBusiId) {
        return success(scanBusiService.selectScanBusiById(scanBusiId));
    }

    /**
     * 新增扫码业务
     */
    @PreAuthorize("@ss.hasPermi('icbc:scanbusi:add')")
    @Log(title = "扫码业务管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TScanBusi scanBusi)
    {
        scanBusi.setBusiId(SeqUtil.getSeqId());

        if (!scanBusiService.checkScanBusiCodeUnique(scanBusi)) {
            return error("新增业务'" + scanBusi.getName() + "'失败，业务编码已存在");
        }

        scanBusi.setCreateBy(String.valueOf(scanBusi.getBusiId()));
        return toAjax(scanBusiService.insertScanBusi(scanBusi));
    }

    /**
     * 修改扫码业务
     */
    @PreAuthorize("@ss.hasPermi('icbc:scanbusi:edit')")
    @Log(title = "扫码业务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TScanBusi scanBusi) {

        if (!scanBusiService.checkScanBusiCodeUnique(scanBusi)) {
            return error("修改业务'" + scanBusi.getName() + "'失败，业务编码已存在");
        }
        scanBusi.setCreateBy(String.valueOf(scanBusi.getBusiId()));
        return toAjax(scanBusiService.updateScanBusi(scanBusi));
    }

    /**
     * 删除扫码业务
     */
    @PreAuthorize("@ss.hasPermi('icbc:scanbusi:remove')")
    @Log(title = "扫码业务管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scanBusiIds}")
    public AjaxResult remove(@PathVariable Long[] scanBusiIds) {
        return toAjax(scanBusiService.deleteScanBusiById(scanBusiIds));
    }

}
